package com.hp.goods_service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.goods_service.entity.Goods;
import com.hp.goods_service.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RefreshScope
@RestController
public class GoodsController {

    //注入配置的端口
//    @Value("${server.port}")
//    private Integer port;
//
//    @Value("${spring.datasource.driver-class-name}")
//    private String driver;
//
//    @Value("${spring.datasource.url}")
//    private String url;
//
//    @Value("${spring.datasource.username}")
//    private String username;
//
//    @Value("${spring.datasource.password}")
//    private String password;

    //模拟数据
//    private static final List<Goods> GOODS_LIST = new ArrayList<>();
//    static{
//        GOODS_LIST.addAll(
//            Arrays.asList(
//                    new Goods(1L,"小米10手机",4444.0),
//                    new Goods(2L,"小米11手机",4444.0),
//                    new Goods(3L,"小米12手机",4444.0),
//                    new Goods(4L,"小米13手机",4444.0),
//                    new Goods(5L,"小米14手机",4444.0)
//            )
//        );
//    }

    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/goods/{id}")
    public ResponseEntity<Goods> getGoodsById(@PathVariable Long id){
        Goods goods = goodsService.getGoodsById(id);
        //模拟数据库查询
//        for (Goods g : GOODS_LIST){
//            if(g.getId() == id){
//                goods = g;
//            }
//        }
//        goods.setName(goods.getName()+port);
//        System.out.println(driver+"--"+url+"--"+username+"--"+password);
        return ResponseEntity.ok(goods);
    }

    @RequestMapping("/goods/page/{current}")
    public ResponseEntity<IPage<Goods>> getGoodsPage(@PathVariable Long current){
        IPage<Goods> goodsPage = goodsService.getGoodsPage(new Page<Goods>(current, 10));
        return ResponseEntity.ok(goodsPage);
    }

    @PostMapping("/goods")
    public ResponseEntity<String> saveGoods(@RequestBody Goods goods){
//        GOODS_LIST.add(goods);
        goodsService.saveGoods(goods);
        return ResponseEntity.ok("ok");
    }

    @PutMapping("/goods")
    public ResponseEntity<String> updateGoods(@RequestBody Goods goods){
//        Goods goods2 = GOODS_LIST
//                .stream()
//                .filter(goods1 -> goods.getId() == goods1.getId())
//                .collect(Collectors.toList()).get(0);
//        goods2.setName(goods.getName());
//        goods2.setPrice(goods.getPrice());
        goodsService.updateGoods(goods);
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("/goods/{id}")
    public ResponseEntity<String> deleteGoods(@PathVariable Long id){
//        Goods goods = GOODS_LIST
//                .stream()
//                .filter(goods1 -> id == goods1.getId())
//                .collect(Collectors.toList()).get(0);
//        GOODS_LIST.remove(goods);
        goodsService.removeGoods(id);
        return ResponseEntity.ok("ok");
    }

    @GetMapping("/goodsbuy")
    public ResponseEntity<Boolean> buy(){
        Boolean buy = goodsService.buy();
        return ResponseEntity.ok(buy);
    }

//    @GetMapping("/goods")
//    public ResponseEntity<List<Goods>> getAllGoods(){
//        return ResponseEntity.ok(GOODS_LIST);
//    }
}
